import UIForm from "../../AzraelFrameWork/RunTime/UI/UIForm";
import GameMainEntry from "../Base/GameMainEntry";
import {ScoreTextGameArgs, StartGameArg} from "../Game/NormalGame";
import ccclass = cc._decorator.ccclass;
import property = cc._decorator.property;
import RigidBody = cc.RigidBody;
import PhysicsBoxCollider = cc.PhysicsBoxCollider;
import RigidBodyType = cc.RigidBodyType;
import {LineTriggerScript} from "../Game/LineTriggerScript";
import BoxCollider = cc.BoxCollider;
import { Subject } from "rxjs";
import {EventID} from "../Event/EventID";
import {UIStruct} from "./UIStruct";


@ccclass
export class UI_fight extends UIForm{

    @property(cc.Label)
    public score:cc.Label = null;
    @property(cc.Vec2)
    private startPos:cc.Vec2 = cc.v2(0,0);
    @property(cc.Vec2)
    private endPos:cc.Vec2 = cc.v2(0,0);

    private _linenode:cc.Node;


    OnClose(isShutdown: boolean, userdata?): void {
        super.OnClose(isShutdown, userdata);
        GameMainEntry.Event.Unsubscribe(EventID.ScoreText,this.updatescoretext.bind(this));
    }

    OnDepthChanged(depth: number): void {
        super.OnDepthChanged(depth);
    }

    OnInit(serialId: number, uiFormAssetName: string, userData?): void {
        super.OnInit(serialId, uiFormAssetName, userData);
        this.SerialId = UIStruct.UI_fight;
        this.score.string = "0";
        this.node.on(cc.Node.EventType.TOUCH_START,this.onTouchStart,this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE,this.onTouchMove,this);
        this.node.on(cc.Node.EventType.TOUCH_END,this.onTouchEnd,this);
        let bg = this.node.getChildByName("bg");
        this._linenode = bg.getChildByName("line");

    }

    OnOpen(userdata?): void {
        super.OnOpen(userdata);
        GameMainEntry.Event.Subscribe(EventID.ScoreText,this.updatescoretext.bind(this));
    }

    OnUpdate(elapseSeconds: number, realElapseSeconds: number): void {
        super.OnUpdate(elapseSeconds, realElapseSeconds);
    }

    onTouchStart(touch:cc.Touch,event){
        this.startPos = touch.getLocation();
        this._linenode.removeAllChildren();
    }

    onTouchMove(touch:cc.Touch,event){
        this.endPos = this._linenode.convertToNodeSpaceAR(touch.getLocation());
        let startpos:cc.Vec2 = this._linenode.convertToNodeSpaceAR(this.startPos);
        let ctx:cc.Graphics =  this._linenode.getComponent(cc.Graphics);
        ctx.clear();
        ctx.strokeColor.a = 50;
        ctx.moveTo(startpos.x,startpos.y);
        ctx.lineTo(this.endPos.x,this.endPos.y);
        ctx.fill();
        ctx.stroke();
    }


    onTouchEnd(touch:cc.Touch,event){
        this.endPos = this._linenode.convertToNodeSpaceAR(touch.getLocation());
        let startpos:cc.Vec2 = this._linenode.convertToNodeSpaceAR(this.startPos);
        let ctx:cc.Graphics =  this._linenode.getComponent(cc.Graphics);
        ctx.strokeColor.a = 255;
        ctx.moveTo(startpos.x,startpos.y);
        ctx.lineTo(this.endPos.x,this.endPos.y);
        ctx.fill();
        ctx.stroke();
        let child = new cc.Node("Box");
        child.setParent(ctx.node);
        child.group = "line";
        let boxCollider:BoxCollider = child.addComponent(BoxCollider);
        let box:PhysicsBoxCollider = child.addComponent(PhysicsBoxCollider);
        child.addComponent(LineTriggerScript);
        let rigidBody:RigidBody = box.body;
        rigidBody.type = RigidBodyType.Static;
        box.enabled = true;
        let startpos1:cc.Vec2 = child.convertToNodeSpaceAR(this.startPos);
        let endpos1:cc.Vec2 = child.convertToNodeSpaceAR(touch.getLocation());
        let dir:cc.Vec2 = endpos1.sub(startpos1);
        let center:cc.Vec2 = startpos1.add(endpos1);
        center = center.div(2);
        let rotate:number = Math.atan(dir.y/dir.x);
        let degree = Math.floor(cc.misc.radiansToDegrees(rotate));
        child.setPosition(center);
        child.angle = degree;
        box.size.height = ctx.lineWidth;
        box.size.width = dir.len();
        boxCollider.size.height = ctx.lineWidth+2;
        boxCollider.size.width = dir.len();
        box.restitution = 1;
        box.friction = 0;
        box.apply();
    }

    updatescoretext(sender,e){
        if(e instanceof ScoreTextGameArgs){
            this.score.string = e.Score.toString();
        }
    }



}